#!/bin/bash

### If using 'accetto/argbash-docker' image, then rebuild as 'argbash parser.rc --strip user-content'
### Then test it as './startup.sh -v' or './startup.sh -h' or './startup.sh -H'

# Created by argbash-init v2.8.1
# ARG_VERSION([echo $(basename $0) v20.12.06])
# ARG_HELP([Container startup script],[Use '-H' or '--help-usage' for extended container usage help.\nFor more information visit https://github.com/accetto/ubuntu-vnc-xfce-g3])
# ARG_OPTIONAL_ACTION([help-usage],[H],[Extended container usage help.],[print_usage])
#
# ARG_POSITIONAL_INF([command],[Optional command with optional arguments. It is executed during startup.])
#
# ARG_POSITIONAL_DOUBLEDASH([])
#
# ARG_OPTIONAL_BOOLEAN([wait],[],[Default background execution mode],[on])
# ARG_OPTIONAL_BOOLEAN([skip-startup],[],[Default foreground execution mode])
#
# ARG_OPTIONAL_BOOLEAN([tail-null],[],[Alternative background execution mode])
# ARG_OPTIONAL_BOOLEAN([tail-vnc],[],[Alternative background execution mode])
#
# ARG_OPTIONAL_BOOLEAN([version-sticker],[],[Alternative foreground execution mode])
# ARG_OPTIONAL_BOOLEAN([version-sticker-verbose],[],[Alternative foreground execution mode])
#
# ARG_OPTIONAL_BOOLEAN([skip-vnc],[],[Startup process modifier])
# ARG_OPTIONAL_BOOLEAN([skip-novnc],[],[Startup process modifier])
# ARG_OPTIONAL_BOOLEAN([debug],[],[Startup process modifier])
# ARG_OPTIONAL_BOOLEAN([verbose],[],[Startup process modifier])
#
# ARGBASH_GO()
# needed because of Argbash --> m4_ignore([
### START OF CODE GENERATED BY Argbash v2.8.1 one line above ###
# Argbash is a bash code generator used to get arguments parsing right.
# Argbash is FREE SOFTWARE, see https://argbash.io for more info


die()
{
	local _ret="${2:-1}"
	test "${_PRINT_HELP:-no}" = yes && print_help >&2
	echo "$1" >&2
	exit "${_ret}"
}


begins_with_short_option()
{
	local first_option all_short_options='vhH'
	first_option="${1:0:1}"
	test "$all_short_options" = "${all_short_options/$first_option/}" && return 1 || return 0
}

# THE DEFAULTS INITIALIZATION - POSITIONALS
_positionals=()
_arg_command=()
# THE DEFAULTS INITIALIZATION - OPTIONALS
_arg_wait="on"
_arg_skip_startup="off"
_arg_tail_null="off"
_arg_tail_vnc="off"
_arg_version_sticker="off"
_arg_version_sticker_verbose="off"
_arg_skip_vnc="off"
_arg_skip_novnc="off"
_arg_debug="off"
_arg_verbose="off"


print_help()
{
	printf '%s\n' "Container startup script"
	printf 'Usage: %s [-v|--version] [-h|--help] [-H|--help-usage] [--(no-)wait] [--(no-)skip-startup] [--(no-)tail-null] [--(no-)tail-vnc] [--(no-)version-sticker] [--(no-)version-sticker-verbose] [--(no-)skip-vnc] [--(no-)skip-novnc] [--(no-)debug] [--(no-)verbose] [--] [<command-1>] ... [<command-n>] ...\n' "$0"
	printf '\t%s\n' "<command>: Optional command with optional arguments. It is executed during startup."
	printf '\t%s\n' "-v, --version: Prints version"
	printf '\t%s\n' "-h, --help: Prints help"
	printf '\t%s\n' "-H, --help-usage: Extended container usage help."
	printf '\t%s\n' "--wait, --no-wait: Default background execution mode (on by default)"
	printf '\t%s\n' "--skip-startup, --no-skip-startup: Default foreground execution mode (off by default)"
	printf '\t%s\n' "--tail-null, --no-tail-null: Alternative background execution mode (off by default)"
	printf '\t%s\n' "--tail-vnc, --no-tail-vnc: Alternative background execution mode (off by default)"
	printf '\t%s\n' "--version-sticker, --no-version-sticker: Alternative foreground execution mode (off by default)"
	printf '\t%s\n' "--version-sticker-verbose, --no-version-sticker-verbose: Alternative foreground execution mode (off by default)"
	printf '\t%s\n' "--skip-vnc, --no-skip-vnc: Startup process modifier (off by default)"
	printf '\t%s\n' "--skip-novnc, --no-skip-novnc: Startup process modifier (off by default)"
	printf '\t%s\n' "--debug, --no-debug: Startup process modifier (off by default)"
	printf '\t%s\n' "--verbose, --no-verbose: Startup process modifier (off by default)"
	printf '\n%s\n' "Use '-H' or '--help-usage' for extended container usage help.
For more information visit https://github.com/accetto/ubuntu-vnc-xfce-g3"
}


parse_commandline()
{
	_positionals_count=0
	while test $# -gt 0
	do
		_key="$1"
		if test "$_key" = '--'
		then
			shift
			test $# -gt 0 || break
			_positionals+=("$@")
			_positionals_count=$((_positionals_count + $#))
			shift $(($# - 1))
			_last_positional="$1"
			break
		fi
		case "$_key" in
			-v|--version)
				echo $(basename $0) v20.12.06
				exit 0
				;;
			-v*)
				echo $(basename $0) v20.12.06
				exit 0
				;;
			-h|--help)
				print_help
				exit 0
				;;
			-h*)
				print_help
				exit 0
				;;
			-H|--help-usage)
				print_usage
				exit 0
				;;
			-H*)
				print_usage
				exit 0
				;;
			--no-wait|--wait)
				_arg_wait="on"
				test "${1:0:5}" = "--no-" && _arg_wait="off"
				;;
			--no-skip-startup|--skip-startup)
				_arg_skip_startup="on"
				test "${1:0:5}" = "--no-" && _arg_skip_startup="off"
				;;
			--no-tail-null|--tail-null)
				_arg_tail_null="on"
				test "${1:0:5}" = "--no-" && _arg_tail_null="off"
				;;
			--no-tail-vnc|--tail-vnc)
				_arg_tail_vnc="on"
				test "${1:0:5}" = "--no-" && _arg_tail_vnc="off"
				;;
			--no-version-sticker|--version-sticker)
				_arg_version_sticker="on"
				test "${1:0:5}" = "--no-" && _arg_version_sticker="off"
				;;
			--no-version-sticker-verbose|--version-sticker-verbose)
				_arg_version_sticker_verbose="on"
				test "${1:0:5}" = "--no-" && _arg_version_sticker_verbose="off"
				;;
			--no-skip-vnc|--skip-vnc)
				_arg_skip_vnc="on"
				test "${1:0:5}" = "--no-" && _arg_skip_vnc="off"
				;;
			--no-skip-novnc|--skip-novnc)
				_arg_skip_novnc="on"
				test "${1:0:5}" = "--no-" && _arg_skip_novnc="off"
				;;
			--no-debug|--debug)
				_arg_debug="on"
				test "${1:0:5}" = "--no-" && _arg_debug="off"
				;;
			--no-verbose|--verbose)
				_arg_verbose="on"
				test "${1:0:5}" = "--no-" && _arg_verbose="off"
				;;
			*)
				_last_positional="$1"
				_positionals+=("$_last_positional")
				_positionals_count=$((_positionals_count + 1))
				;;
		esac
		shift
	done
}


assign_positional_args()
{
	local _positional_name _shift_for=$1
	_positional_names=""
	_our_args=$((${#_positionals[@]} - 0))
	for ((ii = 0; ii < _our_args; ii++))
	do
		_positional_names="$_positional_names _arg_command[$((ii + 0))]"
	done

	shift "$_shift_for"
	for _positional_name in ${_positional_names}
	do
		test $# -gt 0 || break
		eval "$_positional_name=\${1}" || die "Error during argument parsing, possibly an Argbash bug." 1
		shift
	done
}

parse_commandline "$@"
assign_positional_args 1 "${_positionals[@]}"

# OTHER STUFF GENERATED BY Argbash

### END OF CODE GENERATED BY Argbash (sortof) ### ])
